<template>
    <div class="container">
        <div class="newsHeader" :style="{ backgroundImage: `url(${require('@/assets/newsbg.jpg')})` }">

        </div>
        <div class="search">
            <el-popover placement="bottom" title="检索结果" width="50%" :visible="visible">
                <template #reference>
                    <el-input v-model="searchText" class="w-50 m-2" placeholder="请输入新闻关键字" :prefix-icon="Search"
                        type="search" size="large" @input="visible = true" @blur="visible = false" />
                </template>
                <div v-if="searcNewsList.length">
                    <div v-for="item in searcNewsList" :key="item._id" class="search-item"
                        @click="handleChangepage(item._id)">
                        {{ item.title }}
                    </div>
                </div>
                <div v-else>
                    <el-empty description="暂无新闻" :image-size="50" />
                </div>
            </el-popover>
        </div>
        <div class="topNews">
            <el-row :gutter="20">
                <el-col :span="6" v-for="item in topNewsList" :key="item._id">
                    <el-card :body-style="{ padding: '0px' }" shadow="hover" @click="handleChangepage(item._id)">
                        <div class="image" :style="{ backgroundImage: `url(http://localhost:3000${item.cover})` }"></div>
                        <div style="padding: 14px;float: left;">
                            <span>{{ item.title }}</span>
                            <div class="bottom">
                                <time class="time">{{ whichTime(item.editTime) }}</time>
                            </div>
                        </div>
                    </el-card>
                </el-col>
            </el-row>
        </div>
        <el-tabs v-model="whichTab" class="demo-tabs" style="margin: 20px;">
            <el-tab-pane :label="item.label" :name="item.name" v-for="item in  tabList " :key="item.name">
                <el-row :gutter="20">
                    <el-col :span="18">
                        <div v-for="data in tabNews[item.name]" :key="data._id" style="padding: 10px;">
                            <el-card :body-style="{ padding: '0px' }" shadow="hover" @click="handleChangepage(data._id)">
                                <div class="tab-image"
                                    :style="{ backgroundImage: `url(http://localhost:3000${data.cover})` }">
                                </div>
                                <div style="padding: 14px">
                                    <span>{{ data.title }}</span>
                                    <div class="bottom">
                                        <time class="tab-time">{{ whichTime(data.editTime) }}</time>
                                    </div>
                                </div>
                            </el-card>
                        </div>
                    </el-col>
                    <el-col :span="6">
                        <el-timeline>
                            <el-timeline-item v-for="(data, index) in tabNews[item.name]" :key="index"
                                :timestamp="whichTime(data.editTime)">
                                {{ data.title }}
                            </el-timeline-item>
                        </el-timeline>
                    </el-col>
                </el-row>
            </el-tab-pane>
        </el-tabs>
        <el-backtop :visibility-height="100" />
    </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import axios from 'axios';
import _ from 'lodash'
import { ref, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import whichTime from '@/util/util'
const searchText = ref('')
const visible = ref(false)
const newsList = ref([])

const whichTab = ref(1)

onMounted(async () => {
    const res = await axios.get('/webapi/news/list')
    console.log(res.data)
    newsList.value = res.data.data

    console.log(_.groupBy(newsList.value, item => item.category))
})

const searcNewsList = computed(() => {
    if (searchText.value != '') {
        return newsList.value.filter(item => item.title.includes(searchText.value))
    } else {
        return []
    }
})

const topNewsList = computed(() => newsList.value.slice(0, 4))


const tabList = ref([{
    label: '最新动态',
    name: 1
}, {
    label: '典型案例',
    name: 2
}, {
    label: '通知公告',
    name: 3
}])

const tabNews = computed(() => { return _.groupBy(newsList.value, item => item.category) })

const router=useRouter()
const handleChangepage = (id) => {
    console.log(id)
    router.push(`/news/${id}`)
}
</script>

<style scoped lang="scss">
.container {
    position: relative;

    .newsHeader {
        width: 100%;
        height: 400px;
        background-size: cover;
    }

    .search {
        position: absolute;
        top: 300px;
        width: 100%;
        text-align: center;

        .el-input {
            width: 50%;
        }
    }
}

.search-item {
    height: 50px;
    line-height: 50px;

    &:hover {
        background-color: whitesmoke;
        color: red;
    }
}

.topNews {
    margin: 20px;

    .image {
        width: 100%;
        height: 150px;
        background-size: cover;
    }

    .time {
        font-size: 13px;
        color: gray;
    }
}

.tab-image {
    width: 150px;
    height: 100px;
    background-size: cover;
    float: left;
}

.tab-time {
    font-size: 13px;
    color: gray;
}
</style>